<?php
/***********************************************************
	Filename: app/www/control/list.php
	Note	: 内容列表页
	Version : 3.0
	Author  : qinggan
	Update  : 2009-10-16
***********************************************************/
class list_c extends Control
{
	var $subject;
	var $tplfile = "list";
	var $pageurl = "";
	var $idstring = "";
	var $pageid = 0;//当前分页ID号
	function __construct()
	{
		parent::Control();
		$this->load_model("cate");
		$this->load_model("list");
		$this->load_model("module");
	}

	function list_c()
	{
		$this->__construct();
	}

	function index_f()
	{
		$cid = $this->trans_lib->int("cid");
		$cs = $this->trans_lib->safe("cs");
		$mid = $this->trans_lib->int("mid");
		$ms = $this->trans_lib->safe("ms");
		if(!$cid && !$cs && !$mid && !$ms)
		{
			sys_header(HOME_PAGE);
		}
		$pageid = $this->trans_lib->int(SYS_PAGEID);
		$this->pageid = $pageid;
		if($cid || $cs)
		{
			$this->cate_m->set_langid($_SESSION["sys_lang_id"]);
			$this->pageurl = site_url("list","cid=".$cid);
			if($cs && !$cid)
			{
				$cid = $this->cate_m->get_cid_from_code($cs);
			}
			if(!$cid)
			{
				sys_header(HOME_PAGE);
			}
			$this->index_cate($cid);
		}
		else
		{
			$this->pageurl = site_url("list","mid=".$mid);
			if($ms && !$mid)
			{
				$mid = $this->module_m->get_mid_from_code($ms);
			}
			if(!$mid)
			{
				error($this->lang["not_found_any_module"],HOME_PAGE);
				sys_header(HOME_PAGE);
			}
			$this->index_module($mid);
		}
		return true;
	}

	//分类列表页或是封面页
	function index_cate($cid)
	{
		$rs = $this->cate_m->get_one($cid);
		$pageurl = list_url($rs,0,true,false);//
		$module_rs = $this->module_m->get_one($rs["module_id"]);
		//指定模板文件
		if($rs["if_index"])
		{
			$this->tplfile = $rs["tpl_index"] ? $rs["tpl_index"] : "index_".$module_rs["identifier"];
		}
		else
		{
			$this->tplfile = $rs["tpl_list"] ? $rs["tpl_list"] : "list_".$module_rs["identifier"];
		}
		//自定义模板目录
		if($rs["tpl_id"])
		{
			$this->read_tpl_setting($rs["tpl_id"]);
		}
		//分类未启用
		if(!$rs["status"])
		{
			sys_header(HOME_PAGE);
		}
		//分类下的模块未启用
		if(!$module_rs["status"])
		{
			sys_header(HOME_PAGE);
		}
		$this->tpl->assign("rs",$rs);
		//导航信息
		$this->load_cate_msg($rs);
		//通过递归获取子分类ID
		$array = array($cid);
		$this->cate_m->get_sonid_array($array,$cid);
		$idstring = implode(",",$array);
		$this->idstring = $idstring;
		//如果存在分类至主题
		if($module_rs["cate2sub"])
		{
			//读取当前第一个主题
			$rslist = $this->cate_m->get_cate2sub($idstring,$rs["ordertype"]);
			$header_url = $rslist["identifier"] ? site_url("msg","ts=".$rslist["identifier"]) : site_url("msg","id=".$rslist["id"]);
			sys_header($header_url);
		}
		//如果启用封面页功能，将停止下一步操作的执行
		if($rs["if_index"])
		{
			$this->tpl->display($this->tplfile.".".$this->tpl->ext);
		}
		//读取列表数据
		$this->list_m->set_cate($rs);
		$this->list_m->set_idstring($idstring);
		$this->list_m->set_module($module_rs);
		$total = $this->list_m->get_count_from_cate();//取得总数量
		$this->tpl->assign("total",$total);
		$psize = $rs["psize"] ? $rs["psize"] : SYS_PSIZE;
		$offset = $this->pageid>0 ? ($this->pageid-1)*$psize : 0;
		$this->page_lib->set_psize($psize);
		$pagelist = $this->page_lib->page_www($pageurl,$total,true);//分页数组
		$this->tpl->assign("pagelist",$pagelist);
		$rslist = $this->list_m->get_list_from_cate($offset,$psize);
		$this->tpl->assign("rslist",$rslist);
		$this->tpl->display($this->tplfile.".".$this->tpl->ext);
	}

	function index_module($mid)
	{
		$rs = $this->module_m->get_one($mid);
		$this->tplfile = "list_".$rs["identifier"];
		if(!$rs["status"])
		{
			error($this->lang["module_is_close"],HOME_PAGE);
		}
		//通过此模块直接进入主题信息
		//$rslist = $this->cate_m->get_id_from_module($mid);
		//if(!$rslist)
		//{
		//	error($this->lang["not_any_title_in_module"],HOME_PAGE);
		//}
		//分类下的模块未启用
		if(!$rs["status"])
		{
			sys_header(HOME_PAGE);
		}
		//判断是否有分类
		if($rs["cate2sub"])
		{
			$msg_rs = $this->module_m->get_module_sub_one($mid);
			$header_url = msg_url($msg_rs);
		}
		else
		{
			$list_rs = $this->module_m->get_module_cateid($mid);
			if(!$list_rs)
			{
				error($this->lang["not_any_cate_in_module"],HOME_PAGE);
			}
			$header_url = list_url($list_rs);
		}
		sys_header($header_url);
	}

	//重设模板目录
	function read_tpl_setting($tplid=0)
	{
		if(!$tplid || $tplid == intval($this->tpl->tplid))
		{
			return false;
		}
		$this->load_model("tpl");
		$rs = $this->tpl_m->getone($tplid);
		if(!$rs)
		{
			return false;
		}
		$this->tpl->tplid = $tplid;
		$this->tpl->tpldir = ROOT."templates/".$rs["folder"];
		$this->tpl->cache = ROOT."data/tpl_c";
		$this->tpl->ext = $rs["ext"] ? $rs["ext"] : "html";
		$this->tpl->autoimg = $rs["autoimg"];
	}

	function load_cate_msg($rs)
	{
		$array = array();
		$array[0] = $rs;
		if($rs["parentid"])
		{
			$array = $this->cate_m->get_parent_array($array,$rs["parentid"]);
		}
		$rslist = array();
		$site_title_array = array();
		foreach($array AS $key=>$value)
		{
			$tmp = array();
			$tmp["title"] = $value["cate_name"];
			$ext = $value["identifier"] ? "cs=".$value["identifier"] : "id=".$value["id"];
			$tmp["url"] = site_url("list",$ext);
			$rslist[$key] = $tmp;
			$site_title_array[] = $value["cate_name"];
		}
		$sitetitle = implode(" - ",$site_title_array);
		$this->tpl->assign("sitetitle",$sitetitle);
		//倒序数组
		krsort($rslist);
		unset($array);
		$this->tpl->assign("leader",$rslist);
	}
}
?>